window.addEventListener("DOMContentLoaded", function() {
	function setIntervalMethod(fun, timeSpan) {
		setInterval(fun, timeSpan);
	};
	let searchDiv = document.getElementById("searchDiv01");
	function activeLunBo01(objFather, obj, objChildren, timeSpan = 3000) {
		obj.button01 = objFather.getElementsByClassName("prevLi")[0];
		obj.button02 = objFather.getElementsByClassName("nextLi")[0];
		obj.ulLiOrder01 = 0;
		obj.iii = 0;
		obj.flag01 = false;
		obj.flag02 = true; // 用于setInterval函数的启动开锁
		obj.set = null;
		clearInterval(obj.set);

		obj.button01.addEventListener("click", function() {
			if(obj.flag01 === true) {
				obj.ulLiOrder01--;

			} else {
				obj.ulLiOrder01++;

			}

			obj.style.left = (-objChildren[0].offsetWidth * obj.ulLiOrder01) + "px";
			if(obj.ulLiOrder01 === objChildren.length - 1) {
				obj.flag01 = true;

			} else if(obj.ulLiOrder01 === 0) {
				obj.flag01 = false;

			}

		});
		obj.button02.addEventListener("click", function() {

			if(obj.flag01 === true) {
				obj.ulLiOrder01--;

			} else {
				obj.ulLiOrder01++;

			}

			obj.style.left = (-obj.getElementsByTagName("li")[0].offsetWidth * obj.ulLiOrder01) + "px";
			if(obj.ulLiOrder01 === objChildren.length - 1) {
				obj.flag01 = true;

			} else if(obj.ulLiOrder01 === 0) {
				obj.flag01 = false;

			}
		});
		objFather.addEventListener("mouseenter", function() {
			obj.flag02 = false;
			obj.set = null;
			objFather.addEventListener("mouseleave", function() {
				obj.flag02 = true;

			});
		});
		/*obj.set = setInterval(function() {
			
		}, timeSpan);*/
		function forCircle() {
			if(obj.flag02 === true) {
				obj.button01.click();
				for(; obj.iii < objChildren[obj.ulLiOrder01].getElementsByTagName("div").length; obj.iii++) {
					objChildren[obj.ulLiOrder01].getElementsByTagName("div")[obj.iii].style.background = "rgba(78, " + (obj.ulLiOrder01 * 40) + ", 70, 0.65)";

				}
				obj.iii = 0;
			}
		};

		setIntervalMethod(forCircle, timeSpan);
		
		// 我们没有一直待在这个页面，当切换到其他页面时，停止这个页面的定时器
		if(document.hidden) {
			obj.flag02 = false;
		}
		else {
			//当鼠标放在搜索框上时，停止定时器运行
			searchDiv.onmouseover = function() {
				obj.flag02 = false;	
				return;
			}
			searchDiv.onmouseleave = function() {
				obj.flag02 = true;
				return;
			}
			obj.flag02 = true;
		}
	};
	
	
	let showCars = document.getElementsByClassName("show-cars")[0];
	let showCarsUL = showCars.getElementsByTagName("ul")[0];
	let showCarsLi = showCarsUL.getElementsByClassName("car-li");
	// 开始调用轮播图函数
	activeLunBo01(showCars, showCarsUL, showCarsLi, 5000);
	
	//实现点击回到顶部的功能
	let toTop = document.getElementById("toTop");
	let totop = null;
	$(toTop).click(function() {
		
		clearInterval(totop);
		totop = setInterval(function() {
			window.scrollTo(0, document.documentElement.scrollTop - (document.documentElement.scrollTop / 7));
			if(document.documentElement.scrollTop === 0) {
				clearInterval(totop);
				totop = null;
			}
		}, 40);
	});
	window.addEventListener("mousemove", function() {
		clearInterval(totop);
		totop = null;
		
	});
	window.addEventListener("mousewheel", function() {
		clearInterval(totop);
		totop = null;
		
	});
	document.addEventListener("mouseover", function(e) {
		if(e) {
			if(e.target.tagName === "A" && e.target.href) {
				e.target.title = e.target.innerText;
			}
		}
	});
});